<?php

namespace plugin\webman\gateway;

use GatewayWorker\Lib\Gateway;

class Events
{
    public static function onWorkerStart($worker)
    {

    }

    public static function onConnect($client_id)
    {
        dump('有人进来了'.$client_id);
    }

    public static function onWebSocketConnect($client_id, $data)
    {

    }

    public static function onMessage($client_id, $message)
    {
      //  dump('收到消息',$message);
        if($message=='ping'){
            Gateway::sendToClient($client_id, "pang");
        }else{
            $data=json_decode($message,true);
//           dump($data);
            //绑定uid
            if(isset($data['type']) and $data['type']=='bangUid'){
                Gateway::bindUid($client_id,$data['userId']);
            }
            if(isset($data['type']) and $data['type']=='bangGroup'){
                Gateway::joinGroup($client_id,$data['groupId']);
            }
            if(isset($data['lei']) and $data['lei']=='user'){
                if(!Gateway::isUidOnline($data['toUserId']))
                {
                    // 假设有个your_store_fun函数用来保存未读消息(这个函数要自己实现),这里用队列存储
                    dump('没有在线');
                }else{
                    // 在线就转发消息给对应的uid
                  //  dump(4445,$data['toUserId']);
                    Gateway::sendToUid($data['toUserId'], $message);
                }
            }
            if(isset($data['lei']) and $data['lei']=='group'){
//                dump(Gateway::getClientIdListByGroup($group));
                Gateway::sendToGroup($data['toUserId'], $message);
            }
        }
    }

    public static function onClose($client_id)
    {
        dump('有人进走了'.$client_id);
    }

}
